From: Andrew Cooper Date: Wed, 29 Oct 2014 14:09:41 +0000 (+0000) Subject: tools/pygrub: Fix TOCTOU race introduced by c/s 63dcc68 X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~4115 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/%22bookmarks://%22/%22http:/www.example.com/cgi/%22https:/%22bookmarks:/%22?a=commitdiff_plain;h=beb0ef5f747bac4c6464383d74259b14adbfce19;p=xen.git tools/pygrub: Fix TOCTOU race introduced by c/s 63dcc68 In addition, use os.makedirs() which will also create intermediate directories if they don't exist. Signed-off-by: Andrew Cooper CC: Ian Campbell CC: Ian Jackson CC: Wei Liu CC: Konrad Rzeszutek Wilk CC: Olaf Hering Acked-by: Ian Campbell --- diff --git a/tools/pygrub/src/pygrub b/tools/pygrub/src/pygrub index 4cc846fa17..aa7e562e8a 100644 --- a/tools/pygrub/src/pygrub +++ b/tools/pygrub/src/pygrub @@ -13,7 +13,7 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # -import os, sys, string, struct, tempfile, re, traceback, stat +import os, sys, string, struct, tempfile, re, traceback, stat, errno import copy import logging import platform @@ -851,8 +851,14 @@ if __name__ == "__main__": if debug: logging.basicConfig(level=logging.DEBUG) - if not os.path.isdir(output_directory): - os.mkdir(output_directory, 0700) + + try: + os.makedirs(output_directory, 0700) + except OSError,e: + if (e.errno == errno.EEXIST) and os.path.isdir(output_directory): + pass + else: + raise if output is None or output == "-": fd = sys.stdout.fileno()